package com.cn.algorithm02.class01;

import java.util.Random;

/***
 * @author: hels
 * @description:
 * @date: 2022-9-23 12:06
 **/
public class CodeUtil {
    /**
     生成数组-重载
      */
    public static int[] generatorArray() {
        return generatorArray(true);
    }
    public static int[] generatorArray(boolean f) {
        return generatorArray(20, 17, f);
    }
    public static int[] generatorArray(int maxVaule, int maxSize, boolean b) {
        Random random = new Random();
        int[] arr = new int[random.nextInt(maxSize) + 2];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(maxVaule) - random.nextInt(maxVaule);
        }
        if (b == true) {
            System.out.println("随机生成的数组是： ");
            for (int num: arr) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
        return arr;
    }

    /**
     复制数组，数据存储在不同得内存空间
     */
    public static int[] copyArr(int[] arr) {
        int[] newArr = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }

    /**
    比较两个数组值顺序是否一样
     */
    public static boolean compareArr (int[] arr1, int[] arr2) {
        if (arr1.length != arr2.length) {
            return false;
        }
        boolean b = true;
        for (int i = 0; i < arr1.length; i++) {
            b = arr1[i] == arr2[i] ? true : false;
            if (!b) return false;
        }
        return b;
    }

    /**
    交换两个位置
     */
    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    /**
     打印数组
     */
    public static void printArray(int[] arr) {
        for (int i: arr) {
            System.out.print(i + " ");
        }
        System.out.println();
    }
}
